﻿


using System.Net;

namespace Oddo.Concept.Crm.Common.Presentation
{
    using System;
    using System.IO;
    using System.Linq;
    using System.Windows;
    using DevExpress.Xpf.Docking;
    using DevExpress.Xpf.NavBar;
    using Microsoft.Practices.Prism.Modularity;
    using Microsoft.Practices.Prism.Regions;
    using Microsoft.Practices.Prism.UnityExtensions;
    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.InterceptionExtension;
    using Oddo.Concept.Crm.Common.Attributes;
    using Oddo.Concept.Crm.Common.Behaviors;
    using Oddo.Concept.Crm.Common.Diagnostic;
    using Oddo.Concept.Crm.Common.Localization;
    using Oddo.Concept.Crm.Common.Presentation.Infrastructure;
    using Oddo.Concept.Crm.Common.Presentation.Interfaces;
    using Oddo.Concept.Crm.Common.Presentation.Resources;
    using Oddo.Concept.Crm.Common.Presentation.Services;
    using Oddo.Concept.Crm.Common.Presentation.ViewModels;
    using Oddo.Concept.Crm.Common.Presentation.Views;
    using Oddo.Concept.Crm.Common.Security;
    using System.Net.Http;

    public abstract class DefaultBootStrapper : UnityBootstrapper
    {
        private ISplashView _splashView;

        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();
            this.Container.AddNewExtension<Interception>();

            this.Container.RegisterType<IAuthenticationView, AuthenticationView>(new ContainerControlledLifetimeManager());
            this.Container.RegisterType<ISplashView, SplashView>();
            this.Container.RegisterInstance<IErrorReportManager>(new EMailErrorReportManager(), new ContainerControlledLifetimeManager());
            this.Container.RegisterInstance<ITranslationServices>(new DefaultTranslationService(Labels.ResourceManager), new ContainerControlledLifetimeManager());
            this.Container.RegisterType<IMenuService, MenuService>(new ContainerControlledLifetimeManager());
            this.Container.RegisterType<IBarService, BarService>(new ContainerControlledLifetimeManager());
            this.Container.RegisterType<IAuthenticationService, AuthenticationService>(new ContainerControlledLifetimeManager(), new Interceptor<InterfaceInterceptor>(), new InterceptionBehavior<MethodCallTraceBehavior>());
        }

        protected override IRegionBehaviorFactory ConfigureDefaultRegionBehaviors()
        {
            var config =  base.ConfigureDefaultRegionBehaviors();
            return config;
        }
        protected override void InitializeShell()
        {
            base.InitializeShell();

            var authView = Container.TryResolve<IAuthenticationView>() as AuthenticationView;
            if (authView == null) throw new ArgumentException("Cannot resolve instance of type IAuthenticationView");
            var result = authView.ShowDialog();
            if (result.GetValueOrDefault(false) == false)
            {
                Application.Current.Shutdown(0);
                return;
            }

            _splashView = this.Container.Resolve<ISplashView>();
           _splashView.Show();

            AfterShellInitialized();
        }

        protected override IModuleCatalog CreateModuleCatalog()
        {
            return new DirectoryModuleCatalog { ModulePath = Path.GetFullPath(".\\") };
        }

        protected override void InitializeModules()
        {
            base.InitializeModules();

            var auth = Container.Resolve<IAuthenticationService>();
            if (!auth.IsAuthenticated) return;

            var moduleCatalog = this.Container.Resolve<IModuleCatalog>();
            var moduleManager = this.Container.Resolve<IModuleManager>();

            foreach (var moduleInfo in moduleCatalog.Modules.Where(m => m.State == ModuleState.NotStarted))
            {
                var moduleType = Type.GetType(moduleInfo.ModuleType);
                if (moduleType == null) continue;

                var att = Attribute.GetCustomAttribute(moduleType, typeof(ModuleAccessAttribute), true) as ModuleAccessAttribute;
                if (att == null || (att.Roles.Any(role => auth.CurrentIdentity.IsInRole(role) || role == "*")))
                    moduleManager.LoadModule(moduleInfo.ModuleName);
            }

            _splashView.FadeOut();
            _splashView = null;


            ModulesInitializationCompleted();
        }

        protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
        {
            RegionAdapterMappings mappings = base.ConfigureRegionAdapterMappings();
            mappings.RegisterMapping(typeof(NavBarControl), Container.Resolve<NavBarControlAdapter>());
            mappings.RegisterMapping(typeof(DocumentGroup), Container.Resolve<DocumentGroupAdapter>());
            mappings.RegisterMapping(typeof(TabbedGroup), Container.Resolve<TabbedGroupAdapter>());
            return mappings;
        }

        public abstract void AfterShellInitialized();

        public abstract void ModulesInitializationCompleted();
    }
}